package com.example.children.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.children.DTO.StudentClassDTO;
import com.example.children.DTO.TeacherClassDTO;
import com.example.children.Model.ResponseModel;
import com.example.children.Model.Student;
import com.example.children.Model.Teacher;
import com.example.children.Service.TeacherService;
import com.example.children.Util.Tools;
import com.example.children.Util.UUIDGenerator;
import com.example.children.Util.tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


    @RestController
    @RequestMapping("/Teacher")
    public class TeacherController {

        @Autowired
        private TeacherService teacherService;

        @PostMapping("/add")
        public ResponseModel<Teacher> addTeacher(@RequestBody Teacher teacher) {
            System.out.println("进入addTeacher方法：" + teacher.getTeacherName());
            Integer code = Tools.Success;
            String msg = tool.success;
            String uuid = UUIDGenerator.generateNumericUUID(8);
            teacher.setId(uuid);
            teacherService.insertTeacher(teacher);
            return new ResponseModel<>(teacher, code, msg, null);//响应给前端的对象
        }

        @GetMapping("/TeacherAll")
        public ResponseModel<Page<TeacherClassDTO>> getAllTeachers(
                @RequestParam(defaultValue = "1") int pageNo,  // 默认页码为1
                @RequestParam(defaultValue = "10") int pageSize
        ) {
            System.out.println("进入TeacherAll方法：" );
            Integer code = Tools.Success;
            String msg = tool.success;
            // 调用 Service 方法进行分页查询
            Page<TeacherClassDTO> studentPage = teacherService.getTeacherAllWithClassPage(pageNo, pageSize);
            // 判断分页结果为空
            if (studentPage == null || studentPage.getRecords().isEmpty()) {
                msg = tool.fail;
            }
            // 返回分页数据
            return new ResponseModel<>(studentPage, code, msg, null);
        }




        @PostMapping("/update")
        public ResponseModel<Teacher> updateStudent(@RequestBody Teacher teacher) {
            System.out.println(teacher.getId()+"进入updateTeacher方法：" + teacher.getTeacherName());
            Integer code = Tools.Success;
            String msg = tool.success;
            teacherService.updateTeacher(teacher);
            return new ResponseModel<>(teacher, code, msg, null);//响应给前端的对象
        }

        @DeleteMapping("/deleteTeacher")  //删除
        public ResponseModel<Teacher> deleteTeacher(@RequestParam String id) {
            System.out.println("进入deleteTeacher方法：" + id);
            Integer code = Tools.Success;
            String msg = tool.success;
            teacherService.deleteTeacher(id);
            return new ResponseModel<>(null, code, msg, null);//响应给前端的对象

        }



        //查询
        @PostMapping("/ManySelectStudent")
        public ResponseModel<List<TeacherClassDTO>> getStudentsByAge(@RequestBody TeacherClassDTO teacher) {
            System.out.println("ManySelectStudent");
            Integer code = Tools.Success;
            String msg = tool.success;
            List<TeacherClassDTO> students = teacherService.selectToTeachers(teacher);
            if (students == null || students.isEmpty())
                msg = tool.fail;
            return new ResponseModel<>(students, code, msg, null); // 成功响应
        }


    }






